const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const { webpack, DefinePlugin } = require('webpack')

module.exports = {
    entry: './src/main.js',
    mode:'development',
    output:{
        path: path.resolve(__dirname, 'dist'),
        filename:'[name].[contenthash].js'
    },
    devServer: {
        contentBase: path.join(__dirname, 'dist'),
        compress: true,
        port: 9000,
        hot:true,
    },
    optimization: {
        runtimeChunk: 'single',
      },
    module:{
        rules:[
            {
                // 转换 less文件
                test: /\.less$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'less-loader',
                ]
            },
            {
                test: /\.vue$/,
                use:[
                    'vue-loader'
                ]
            },
            {
                test: /\.js$/,
                loader: 'babel-loader'
            },
            {
                test: /\.css$/,
                use: [
                  'vue-style-loader',
                  'css-loader'
                ]
            },
            {
                test: /\.html$/i,
                loader: 'html-loader',
            },
            {
                test: /\.(png|svg|jpg|jpeg|gif)$/i,
                type: 'asset/resource',
            }
        ]
    },
    plugins:[
        // new webpack.DefinePlugin({
        //     // 值要求的是一个代码片段
        //     BASE_URL: JSON.stringify('./')
        //   }),
        new CleanWebpackPlugin(),
        new VueLoaderPlugin(),
        new HtmlWebpackPlugin({
            title:'Webpack Demo',
            template:'./public/index.html'
        }),
        new CopyWebpackPlugin({
            patterns:[{
                from:'public',
                to:path.resolve(__dirname, 'dist'),
                globOptions: {
                    ignore: ["**/index.html"],
                  },
            }]
        }),
    ]

}